package edu.fpa.day0718;

public class Test05<E> {
    private Node first;
    private Node last;

    private int size;

    public void add(int index,E element) {
        checkRange(index);

        Node newNode = new Node(element);
        Node temp = getNode(index);
        if(temp!=null) {
            Node up = temp.previous;

            up.next = newNode;
            newNode.previous = up;

            newNode.next = temp;
            temp.previous = newNode;
        }

    }

    //remove
    public void remove(int index) {
        checkRange(index);

        Node temp = getNode(index);
        if(temp!=null) {
            Node up = temp.previous;
            Node down = temp.next;

            if(up!=null) {
                up.next = down;

            }
            if(down!=null) {
                down.previous = up;

            }
            //被删除的元素是第一个时
            if(index==0) {
                first = down;
            }
            size--;
        }
    }

    //[]
    //['a','b','c','d','e']
    //get方法
    public E get(int index) {
        checkRange(index);

        Node temp = getNode(index);
        return temp!=null?(E)temp.element:null;
    }

    private void checkRange(int index) {
        if(index<0||index>size-1) {
            throw new RuntimeException("索引数字不合法："+index);
        }
    }

    private Node getNode(int index) {
        checkRange(index);

        Node temp = null;
        if(index<=(size>>1)) {    //size>>1相当于除以2
            temp = first;
            for(int i=0;i<index;i++) {
                temp = temp.next;
            }
        }else {
            temp = last;
            for(int i= size-1;i>index;i--) {
                temp = temp.previous;
            }
        }

        return temp;
    }

    //[]
    //['a']
    public void add(E element) {
        Node node = new Node(element);

        if(first==null) {       //将链表与之联系
            first = node;
            last = node;
        }else {           //添加加后续元素
            node.previous = last;
            node.next = null;

            last.next = node;
            last = node;
        }

        size++;
    }

    @Override
    public String toString() {
        //[a,b,c]  first=a  last=c
        //a,b,c
        StringBuilder sb = new StringBuilder("[");
        Node temp = first;
        while(temp!=null) {
            sb.append(temp.element+",");
            temp = temp.next;
        }
        sb.setCharAt(sb.length()-1, ']');
        return sb.toString();
    }


    public static void main(String[] args) {
        Test05<String> list = new Test05<>();

        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("e");

        System.out.println(list);
        System.out.println(list.get(4));
    }
}
